<template>
  <div class="setting-container">
    <!-- 卡片 -->
    <el-card>
      <!-- tabs -->

      <el-tabs v-model="activeName">
        <!-- 角色管理 -->
        <el-tab-pane label="角色管理" name="first">
          <!-- 老写法 -->
          <!-- <span slot="label"> 角色的管理</span> -->
          <!-- 2.6之后推荐的写法 -->
          <template v-slot:label>
            <span> 角色管理</span>
          </template>
          <!-- 按钮 -->
          <el-button
            icon="el-icon-plus"
            type="primary"
            @click="openDialog"
          >新增角色</el-button>
          <!-- table -->
          <el-table
            v-loading="loading"
            border
            :data="tableData"
            style="width: 100%"
          >
            <el-table-column type="index" label="序号" width="180" />
            <el-table-column sortable prop="name" label="角色名" width="180" />
            <el-table-column sortable prop="description" label="描述" />
            <el-table-column width="300" label="操作">
              <!-- slot-scope="scope" 已抛弃的作用域插槽 -->
              <template v-slot:default="scope">
                <el-button
                  type="text"
                  @click="openPermDialog(scope.row.id)"
                >分配权限</el-button>
                <el-button
                  type="text"
                  @click="editHandle(scope.row.id)"
                >修改</el-button>

                <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  @onConfirm="deleteHandle(scope.row.id)"
                >
                  <el-button slot="reference" type="text">删除</el-button>
                </el-popconfirm>

                <!-- <el-button
                  type="text"
                  @click="clearRole(scope.row.id)"
                >删除</el-button> -->
              </template>
            </el-table-column>
          </el-table>

          <!-- 分页 -->
          <!-- :page-sizes="pageSizes" -->
          <el-pagination
            :current-page.sync="currentPage"
            :page-size.sync="pageSize"
            layout="total, prev, pager, next"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </el-tab-pane>
        <!-- 公司信息 -->
        <el-tab-pane label="公司信息" name="second">
          <template v-slot:label>
            <span> 公司信息</span>
          </template>
          <settingForm />
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <!-- settingDialog -->
    <settingDialog ref="Dialog" />
    <!-- permDialog -->
    <permDialog ref="permDialog" />
  </div>
</template>

<script>
import settingForm from './components/settingForm.vue'
import settingDialog from './components/settingDialog.vue'
import permDialog from './components/permDialog.vue'
import { getRoles, getRoleByid, removeRoleByid } from '../../api/setting'
export default {
  name: 'Setting',
  components: {
    settingForm,
    settingDialog,
    permDialog
  },

  data() {
    return {
      currentPage: 1,

      pageSize: 3,
      total: 5,
      activeName: 'first',
      tableData: [],
      loading: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 打开权限设置弹框
    openPermDialog(id) {
      this.$refs.permDialog.getPermData(id)
    },
    async getData() {
      this.loading = true
      const res = await getRoles({
        page: this.currentPage,
        pagesize: this.pageSize
      })
      // console.log(res)
      this.tableData = res.rows
      this.total = res.total
      setTimeout(() => {
        this.loading = false
      }, 300)
    },
    // 页码改变
    // val 当前页码
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.getData()
    },
    // 新增打开弹窗
    openDialog() {
      this.$refs.Dialog.dialogFormVisible = true
    },
    // 编辑角色 打开弹窗 渲染数据
    async editHandle(id) {
      this.$refs.Dialog.dialogFormVisible = true
      const res = await getRoleByid(id)
      this.$refs.Dialog.form = res
    },
    // 泡泡弹框
    async deleteHandle(id) {
      await removeRoleByid(id)
      if (this.tableData.length === 1 && this.currentPage > 1) {
        this.currentPage--
      }
      this.getData()
      this.$message.success('删除成功')
    }
  }
}
</script>

<style lang="scss">
.setting-container {
  padding: 20px;
  background-color: #eee;
  .el-tabs__item {
    font-size: 20px;
    span {
      padding: 0 20px;
    }
  }
  .el-table {
    thead {
      th {
        background: #ccc;
      }
    }
  }
  .el-pagination {
    text-align: right;
  }
}
</style>
